package org.comet.auxiliary.client.auto.palm;

import lombok.Data;
import org.comet.auxiliary.common.swing.SwingHelper;

import java.util.concurrent.ConcurrentHashMap;

/**
 * @author wq
 * @date 2019/10/9
 * @description 统计帮助类
 * 用于统计累计发送及领取红包金额
 */
public class PacketCountHelper {

    private static PacketStatistics total = new PacketStatistics(0, 0, 0, 0, 0, 0);

    private static ConcurrentHashMap<String, PacketStatistics> sessionCount = new ConcurrentHashMap<>(16);

    @Data
    public static class PacketStatistics {
        private Integer sendAmtFen;
        private Integer takeAmtFen;
        private Integer readAmtFen;

        private Integer takeCount;
        private Integer sendCount;
        private Integer readCount;

        public PacketStatistics(Integer sendAmtFen, Integer takeAmtFen, Integer readAmtFen, Integer takeCount, Integer sendCount, Integer readCount) {
            this.sendAmtFen = sendAmtFen;
            this.takeAmtFen = takeAmtFen;
            this.readAmtFen = readAmtFen;
            this.takeCount = takeCount;
            this.sendCount = sendCount;
            this.readCount = readCount;
        }
    }

    /**
     * 添加发送总金额
     */
    public static void addTotalSendAmt(int add) {
        total.sendAmtFen += add;
        SwingHelper.setTotal(total.sendAmtFen, total.takeAmtFen);
    }

    /**
     * 添加领取总金额
     */
    public static void addTotalTakeAmt(int add) {
        total.takeAmtFen += add;
        SwingHelper.setTotal(total.sendAmtFen, total.takeAmtFen);
    }

    /**
     * 添加读取(接受到消息)总金额, 在发包拖中无用处
     */
    public static void addTotalReadAmt(int add) {
        total.readAmtFen += add;
    }

    /**
     * 添加发送总数
     */
    public static void addTotalSendCount(int add) {
        total.sendCount += add;
    }

    /**
     * 添加领取总数
     */
    public static void addTotalTakeCount(int add) {
        total.takeCount += add;
    }

    /**
     * 添加读取总数
     */
    public static void addTotalReadCount(int add) {
        total.readCount += add;
    }

    /**
     * 以下同上, 只是通过 sessionKey 标识了不同发送人和接收人
     */
    public static void addSessionSendCount(String sessionKey, int add) {
        PacketStatistics sessionPacket = getSessionPacket(sessionKey);
        sessionPacket.sendCount += add;
        addTotalSendCount(add);
    }

    public static void addSessionTakeCount(String sessionKey, int add) {
        PacketStatistics sessionPacket = getSessionPacket(sessionKey);
        sessionPacket.takeCount += add;
        addTotalTakeCount(add);
    }

    public static void addSessionReadCount(String sessionKey, int add) {
        PacketStatistics sessionPacket = getSessionPacket(sessionKey);
        sessionPacket.readCount += add;
        addTotalReadCount(add);
    }

    public static void addSessionSendAmt(String sessionKey, int add) {
        PacketStatistics sessionPacket = getSessionPacket(sessionKey);
        sessionPacket.sendAmtFen += add;
        addTotalSendAmt(add);
    }

    public static void addSessionTakeAmt(String sessionKey, int add) {
        PacketStatistics sessionPacket = getSessionPacket(sessionKey);
        sessionPacket.takeAmtFen += add;
        addTotalTakeAmt(add);
    }

    public static void addSessionReadAmt(String sessionKey, int add) {
        PacketStatistics sessionPacket = getSessionPacket(sessionKey);
        sessionPacket.readAmtFen += add;
        addTotalReadAmt(add);
    }

    public static PacketStatistics getSessionPacket(String sessionKey) {
        boolean exists = sessionCount.containsKey(sessionKey);
        if (!exists) {
            sessionCount.put(sessionKey, new PacketStatistics(0, 0, 0, 0, 0, 0));
        }
        return sessionCount.get(sessionKey);
    }

    public static void clearSession(String sessionKey) {
        sessionCount.put(sessionKey, new PacketStatistics(0, 0, 0, 0, 0, 0));
    }

    public static PacketStatistics getTotalPacket() {
        return total;
    }
}
